<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>

    </style>
</head>

<body>
<div id="app">
    <template>
        <el-input v-model="queryInfo.name" placeholder="请输入名字" style="display: inline-block;width: 200px"></el-input>
        <el-input v-model="queryInfo.minAge" placeholder="请输入最小年龄"style="display: inline-block;width: 200px"></el-input>
        <el-input  v-model="queryInfo.maxAge" placeholder="请输入最大年龄"style="display: inline-block;width: 200px"></el-input>
        <el-button icon="el-icon-search" @click="searchData()" circle></el-button>
        <el-button icon="el-icon-search" circle></el-button>
        <el-button icon="el-icon-search" circle></el-button>
        <el-button type="success" icon="el-icon-check" circle @click="openAddStu()"></el-button>
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column
                    prop="id"
                    label="编号"
                   >
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="姓名"
                   >
            </el-table-column>
            <el-table-column
                    prop="age"
                    label="年龄">
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="地址">
            </el-table-column>
            <el-table-column
                    prop="grade.gname"
                    label="班级">
            </el-table-column>
            <el-table-column
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" circle @click="openUpdateStu(scope.row.id)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" circle @click="deleteStudent(scope.row.id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="1"
                :page-sizes="[5,10,15,20]"
                :page-size="size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="t">
        </el-pagination>
    </template>
    <el-dialog :title="dialogtitle" :visible.sync="dialogFormVisible">
        <el-form :model="stuInfo" :rules="rules" status-icon  ref="stuInfo">
            <el-form-item label="学生姓名" prop="name" >
                <el-input  autocomplete="off" v-model="stuInfo.name"></el-input>
            </el-form-item>
            <el-form-item label="学生年龄" prop="age" >
                <el-input  autocomplete="off" v-model="stuInfo.age"></el-input>
            </el-form-item>
            <el-form-item label="学生地址" prop="address">
                <el-input  autocomplete="off" v-model="stuInfo.address"></el-input>
            </el-form-item>
            <el-form-item label="学生班级"  prop="gid">
                <el-select  placeholder="请选择班级"  v-model="stuInfo.gid">
                    <el-option v-for="g in gradeArr" :label="g.gname" :value="g.id"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addStudent()">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script>
    var app=new Vue({
        el:"#app",
        data:{
            dialogtitle:"",
            queryInfo:{},
            tableData: [],
            size:5,
            t:100,
            dialogFormVisible:false,
            gradeArr:[],
            stuInfo:{

            },
            rules: {
                name: [
                    {
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    }],
                age: [
                    {
                        required: true,
                        message: '请输入年龄',
                        trigger: 'blur'
                    }],
                address: [
                    {
                        required: true,
                        message: '请输入地址',
                        trigger: 'blur'
                    }],
                gid: [
                    {
                        required: true,
                        message: '请输入班级',
                        trigger: 'blur'
                    }]
            }
        },
        methods:{
            openUpdateStu(id){
                $.get("/ssm/grade/list",function (backData){
                    app.gradeArr=backData.data;
                    $.get("/ssm/stu/get/"+id,function (backData){
                       app.stuInfo=backData.data;
                        app.dialogtitle="修改学生";
                        app.dialogFormVisible=true;
                    })
                })
            },
            addStudent(){
                this.$refs["stuInfo"].validate((valid) => {
                    if (valid) {
                        var path=app.dialogtitle=="修改学生" ? "/ssm/stu/update" : "/ssm/stu/add";
                        $.post(path,app.stuInfo,function (backData){
                            if(backData.code==200){
                                app.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });
                                window.location.reload();
                            }else {
                                app.$message({
                                    type: 'danger',
                                    message: '操作失败!'
                                });
                            }
                        })
                    }

                });


            },
            openAddStu(){
                //请求班级的数据
                $.get("/ssm/grade/list",function (backData){
                    app.gradeArr=backData.data;
                    app.stuInfo={};
                    app.dialogtitle="添加学生";
                    app.dialogFormVisible=true;
                })
            },
            searchData(){
                getStudentData(1,5);
            },
            handleSizeChange(val){
                app.size=val;
                getStudentData(1,val);
            },
            handleCurrentChange(val){
                getStudentData(val,app.size);
            },
            deleteStudent(id){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/ssm/stu/delete/"+id,function (backData){
                        if(backData.code==200){
                            app.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            window.location.reload();
                        }else {
                            app.$message({
                                type: 'danger',
                                message: '删除失败!'
                            });
                        }
                    })
                });
            }
        }
    });
    function getStudentData(a,b){
        app.queryInfo.pageNo=a;
        app.queryInfo.pageCount=b;
        $.post("/ssm/stu/query",app.queryInfo,function (backData){
            app.tableData=backData.data.currentData;
            app.t=backData.data.studentstotal;
        })
    }
    getStudentData(1,5);

</script>
</body>
</html>